<?php
/**
 * Created by PhpStorm.
 * User: yy
 * Date: 2019/4/8
 * Time: 11:49
 */

namespace app\common\service\airquality;

use app\common\service\BaseService;

class AqiService extends BaseService
{
    /**
     * 计算AQI
     * 日AQI的计算,臭氧O3按日最大1h平均计算，其他按24h平均计算
     * 小时AQI的计算，SO2的1h平均浓度超过800ug/m3时，用24小时滑动平均浓度值及标准计算IAQI,O3的8h滑动平均暂时不考虑,PM2.5和PM10取当前小时和之前23h的均值，超出最大限值（除表格声明不计算外）按最大限值走
     * $Cp_arr格式 ['pm10_average'=>10,'pm25_average'=>10,'so2_average'=>10,'no2_average'=>10,'co_average'=>10,'o3_average_max'=>10,'o3_8h_average_max'=>10]
     * @author yy by 2019/3/28
     * @param array $Cp_arr 六参污染物浓度
     * @param bool $is_primary_pollutant 是否获取首要污染物
     * @param bool $is_non_attainment_pollutant 是否获取超标污染物
     * @param bool $is_hour 是否小时类型
     * @return mixed
     */
    public function getAqi($Cp_arr = [], $is_primary_pollutant = false, $is_non_attainment_pollutant = false, $is_hour = false)
    {
        # 判断浓度是否超过对应污染物的最大限值
        $IAQIpm10 = $this->getIAQIp('pm10', $Cp_arr['pm10_average']);
        $IAQIpm25 = $this->getIAQIp('pm25', $Cp_arr['pm25_average']);
        if ($is_hour) {
            # so2 1h平均浓度值高于800μg/m³,则实时报中用当天24小时平均浓度值进行其空气质量分指数计算 #用天数据补入
            $IAQIso2 = ($Cp_arr['so2_average'] > 800) ? 0 : $this->getIAQIp('so2', $Cp_arr['so2_average'], true);
            $IAQIno2 = $this->getIAQIp('no2', $Cp_arr['no2_average'], true);
            $IAQIco  = $this->getIAQIp('co', $Cp_arr['co_average'], true);
            $IAQIo3  = $this->getIAQIp('o3', $Cp_arr['o3_average_max'], true);
            # o3_8h 平均浓度值高于800μg/m³,则实时报中用当前1小时平均浓度值进行其空气质量分指数计算
            $IAQIo3_8h = ($Cp_arr['o3_8h_average_max'] > 800) ? $IAQIo3 : $this->getIAQIp('o3_8h', $Cp_arr['o3_8h_average_max'], true);
        } else {
            $IAQIso2 = $this->getIAQIp('so2', $Cp_arr['so2_average']);
            $IAQIno2 = $this->getIAQIp('no2', $Cp_arr['no2_average']);
            $IAQIco  = $this->getIAQIp('co', $Cp_arr['co_average']);
            $IAQIo3  = $this->getIAQIp('o3', $Cp_arr['o3_average_max']);
            # o3_8h 平均浓度值高于800μg/m³,则实时报中用当天24小时平均浓度值中最大1h按照其1h标准进行空气质量分指数计算
            $IAQIo3_8h = ($Cp_arr['o3_8h_average_max'] > 800) ? $IAQIo3 : $this->getIAQIp('o3_8h', $Cp_arr['o3_8h_average_max']);
        }
        $arr['aqi'] = ceil(max($IAQIpm10, $IAQIpm25, $IAQIso2, $IAQIno2, $IAQIco, $IAQIo3, $IAQIo3_8h));
        #获取首要污染物(可能多个) 包括O3_8h
        $arr_IAQI = ['PM10' => $IAQIpm10, 'PM2.5' => $IAQIpm25, 'SO2' => $IAQIso2, 'NO2' => $IAQIno2, 'CO' => $IAQIco, 'O3' => $IAQIo3, 'O3_8h' => $IAQIo3_8h];
        if ($is_primary_pollutant) {
            if ($arr['aqi'] > 50) {
                $max_IAQI         = array_search(max($arr_IAQI), $arr_IAQI);
                $primaryPollutant = [];
                foreach ($arr_IAQI as $k => $v) {
                    if ($v == $arr_IAQI[$max_IAQI]) {
                        $primaryPollutant[] = $k;
                    }
                }
                $arr['primary_pollutant'] = $primaryPollutant;
            } else {
                $arr['primary_pollutant'] = [];
            }
        }
        #获取超标污染物(可能多个)
        if ($is_non_attainment_pollutant) {
            $nonAttainmentPollutant = [];
            foreach ($arr_IAQI as $k => $v) {
                if ($v > 100) {
                    $nonAttainmentPollutant[] = $k;
                }
            }
            $arr['non_attainment_pollutant'] = $nonAttainmentPollutant;
        }
        return $arr;
    }

    /**
     * 取首要污染物（AQI大于50时IAQI最大的污染物）
     * @author yy by 2019/6/4
     * @param array $Cp_arr 格式 ['pm10_average'=>10,'pm25_average'=>10,'so2_average'=>10,'no2_average'=>10,'co_average'=>10,'o3_average_max'=>10,'o3_8h_average_max'=>10]
     * @param bool $is_hour 是否小时类型
     * @return mixed
     */
    public function getPrimaryPollutant($Cp_arr = [], $is_hour = false)
    {
        $primaryPollutant = $this->getAqi($Cp_arr, true, false, $is_hour);
        return $primaryPollutant;
    }

    /**
     * 获取超标污染物（IAQI大于100的污染物）
     * @author yy by 2019/4/8
     * @param array $Cp_arr 六参污染物浓度 格式 ['pm10_average'=>10,'pm25_average'=>10,'so2_average'=>10,'no2_average'=>10,'co_average'=>10,'o3_average_max'=>10,'o3_8h_average_max'=>10]
     * @param bool $is_hour 是否小时类型
     * @return mixed
     */
    public function getNonAttainmentPollutant($Cp_arr = [], $is_hour = false)
    {
        $nonAttainmentPollutant = $this->getAqi($Cp_arr, false, true, $is_hour);
        return $nonAttainmentPollutant;
    }

    /**
     * 单个污染物的空气质量分指数
     * @author yy by 2019/3/28
     * @param $name 污染物名称 pm10 pm25 no2 co so2 o3 o3_8h
     * @param $Cp 污染物浓度
     * @param bool $is_hour 是否小时类型
     * @return float|int
     */
    public function getIAQIp($name, $Cp, $is_hour = false)
    {
        # 处理浓度为null 、小于0和 ''的情况(-9999数据处理人员将其处理为null),0的情况经过计算结果都为0;
        if ($Cp <= 0 || empty($Cp) || is_null($Cp)) {
            return 0;
        }
        $BPhi   = $this->getBPhi($name, $Cp, $is_hour);
        $BPlo   = $this->getBPlo($name, $Cp, $is_hour);
        $IAQIhi = $this->getIAQIhi($name, $Cp, $is_hour);
        $IAQIlo = $this->getIAQIlo($name, $Cp, $is_hour);
        $IAQIp  = (($IAQIhi - $IAQIlo) / ($BPhi - $BPlo)) * ($Cp - $BPlo) + $IAQIlo;
        return $IAQIp;
    }

    /**
     * 表中与Cp相近的污染物浓度限值的高位值
     * @author yy by 2019/3/28
     * @param $name 污染物名称 pm10 pm25 no2 co so2 o3 o3_8h
     * @param $Cp 污染物浓度
     * @param bool $is_hour 是否小时类型
     * @return int
     */
    public function getBPhi($name, $Cp, $is_hour = false)
    {
        switch ($name) {
            case 'pm10':
                switch ($Cp) {
                    case $Cp > 500:
                        return 600;
                        break;
                    case $Cp > 420:
                        return 500;
                        break;
                    case $Cp > 350:
                        return 420;
                        break;
                    case $Cp > 250:
                        return 350;
                        break;
                    case $Cp > 150:
                        return 250;
                        break;
                    case $Cp > 50:
                        return 150;
                        break;
                    case $Cp > 0:
                        return 50;
                        break;
                }
                break;
            case 'pm25':
                switch ($Cp) {
                    case $Cp > 350:
                        return 500;
                        break;
                    case $Cp > 250:
                        return 350;
                        break;
                    case $Cp > 150:
                        return 250;
                        break;
                    case $Cp > 115:
                        return 150;
                        break;
                    case $Cp > 75:
                        return 115;
                        break;
                    case $Cp > 35:
                        return 75;
                        break;
                    case $Cp > 0:
                        return 35;
                        break;
                }
                break;
            case 'so2':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 650:
                            return 800;
                            break;
                        case $Cp > 500:
                            return 650;
                            break;
                        case $Cp > 150:
                            return 500;
                            break;
                        case $Cp > 0:
                            return 150;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 2100:
                            return 2620;
                            break;
                        case $Cp > 1600:
                            return 2100;
                            break;
                        case $Cp > 800:
                            return 1600;
                            break;
                        case $Cp > 475:
                            return 800;
                            break;
                        case $Cp > 150:
                            return 475;
                            break;
                        case $Cp > 50:
                            return 150;
                            break;
                        case $Cp > 0:
                            return 50;
                            break;
                    }
                }
                break;
            case 'no2':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 3090:
                            return 3840;
                            break;
                        case $Cp > 2340:
                            return 3090;
                            break;
                        case $Cp > 1200:
                            return 2340;
                            break;
                        case $Cp > 700:
                            return 1200;
                            break;
                        case $Cp > 200:
                            return 700;
                            break;
                        case $Cp > 100:
                            return 200;
                            break;
                        case $Cp > 0:
                            return 100;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 750:
                            return 940;
                            break;
                        case $Cp > 565:
                            return 750;
                            break;
                        case $Cp > 280:
                            return 565;
                            break;
                        case $Cp > 180:
                            return 280;
                            break;
                        case $Cp > 80:
                            return 180;
                            break;
                        case $Cp > 40:
                            return 80;
                            break;
                        case $Cp > 0:
                            return 40;
                            break;
                    }
                }
                break;
            case 'co':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 120:
                            return 150;
                            break;
                        case $Cp > 90:
                            return 120;
                            break;
                        case $Cp > 60:
                            return 90;
                            break;
                        case $Cp > 35:
                            return 60;
                            break;
                        case $Cp > 10:
                            return 35;
                            break;
                        case $Cp > 5:
                            return 10;
                            break;
                        case $Cp > 0:
                            return 5;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 48:
                            return 60;
                            break;
                        case $Cp > 36:
                            return 48;
                            break;
                        case $Cp > 24:
                            return 36;
                            break;
                        case $Cp > 14:
                            return 24;
                            break;
                        case $Cp > 4:
                            return 14;
                            break;
                        case $Cp > 2:
                            return 4;
                            break;
                        case $Cp > 0:
                            return 2;
                            break;
                    }
                }
                break;
            case 'o3':
                switch ($Cp) {
                    case $Cp > 1000:
                        return 1200;
                        break;
                    case $Cp > 800:
                        return 1000;
                        break;
                    case $Cp > 400:
                        return 800;
                        break;
                    case $Cp > 300:
                        return 400;
                        break;
                    case $Cp > 200:
                        return 300;
                        break;
                    case $Cp > 160:
                        return 200;
                        break;
                    case $Cp > 0:
                        return 160;
                        break;
                }
                break;
            case 'o3_8h':
                switch ($Cp) {
                    case $Cp > 265:
                        return 800;
                        break;
                    case $Cp > 215:
                        return 265;
                        break;
                    case $Cp > 160:
                        return 215;
                        break;
                    case $Cp > 100:
                        return 160;
                        break;
                    case $Cp > 0:
                        return 100;
                        break;
                }
                break;

        }
    }

    /**
     * 表中与Cp相近的污染物浓度限值的低位值
     * @author yy by 2019/3/28
     * @param $name 污染物名称 pm10 pm25 no2 co so2 o3 o3_8h
     * @param $Cp 污染物浓度
     * @param bool $is_hour 是否小时类型
     * @return int
     */
    public function getBPlo($name, $Cp, $is_hour = false)
    {
        switch ($name) {
            case 'pm10':
                switch ($Cp) {
                    case $Cp > 500:
                        return 500;
                        break;
                    case $Cp > 420:
                        return 420;
                        break;
                    case $Cp > 350:
                        return 350;
                        break;
                    case $Cp > 250:
                        return 250;
                        break;
                    case $Cp > 150:
                        return 150;
                        break;
                    case $Cp > 50:
                        return 50;
                        break;
                    case $Cp > 0:
                        return 0;
                        break;
                }
                break;
            case 'pm25':
                switch ($Cp) {
                    case $Cp > 350:
                        return 350;
                        break;
                    case $Cp > 250:
                        return 250;
                        break;
                    case $Cp > 150:
                        return 150;
                        break;
                    case $Cp > 115:
                        return 115;
                        break;
                    case $Cp > 75:
                        return 75;
                        break;
                    case $Cp > 35:
                        return 35;
                        break;
                    case $Cp > 0:
                        return 0;
                        break;
                }
                break;
            case 'so2':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 650:
                            return 650;
                            break;
                        case $Cp > 500:
                            return 500;
                            break;
                        case $Cp > 150:
                            return 150;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 2100:
                            return 2100;
                            break;
                        case $Cp > 1600:
                            return 1600;
                            break;
                        case $Cp > 800:
                            return 800;
                            break;
                        case $Cp > 475:
                            return 475;
                            break;
                        case $Cp > 150:
                            return 150;
                            break;
                        case $Cp > 50:
                            return 50;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                }
                break;
            case 'no2':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 3090:
                            return 3090;
                            break;
                        case $Cp > 2340:
                            return 2340;
                            break;
                        case $Cp > 1200:
                            return 1200;
                            break;
                        case $Cp > 700:
                            return 700;
                            break;
                        case $Cp > 200:
                            return 200;
                            break;
                        case $Cp > 100:
                            return 100;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 750:
                            return 750;
                            break;
                        case $Cp > 565:
                            return 565;
                            break;
                        case $Cp > 280:
                            return 280;
                            break;
                        case $Cp > 180:
                            return 180;
                            break;
                        case $Cp > 80:
                            return 80;
                            break;
                        case $Cp > 40:
                            return 40;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                }
                break;
            case 'co':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 120:
                            return 120;
                            break;
                        case $Cp > 90:
                            return 90;
                            break;
                        case $Cp > 60:
                            return 60;
                            break;
                        case $Cp > 35:
                            return 35;
                            break;
                        case $Cp > 10:
                            return 10;
                            break;
                        case $Cp > 5:
                            return 5;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 48:
                            return 48;
                            break;
                        case $Cp > 36:
                            return 36;
                            break;
                        case $Cp > 24:
                            return 24;
                            break;
                        case $Cp > 14:
                            return 14;
                            break;
                        case $Cp > 4:
                            return 4;
                            break;
                        case $Cp > 2:
                            return 2;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                }
                break;
            case 'o3':
                switch ($Cp) {
                    case $Cp > 1000:
                        return 1000;
                        break;
                    case $Cp > 800:
                        return 800;
                        break;
                    case $Cp > 400:
                        return 400;
                        break;
                    case $Cp > 300:
                        return 300;
                        break;
                    case $Cp > 200:
                        return 200;
                        break;
                    case $Cp > 160:
                        return 160;
                        break;
                    case $Cp > 0:
                        return 0;
                        break;
                }
                break;
            case 'o3_8h':
                switch ($Cp) {
                    case $Cp > 265:
                        return 265;
                        break;
                    case $Cp > 215:
                        return 215;
                        break;
                    case $Cp > 160:
                        return 160;
                        break;
                    case $Cp > 100:
                        return 100;
                        break;
                    case $Cp > 0:
                        return 0;
                        break;
                }
                break;
        }
    }

    /**
     * 表中与BPhi对应的空气质量分指数
     * @author yy by 2019/3/28
     * @param $name 污染物名称 pm10 pm25 no2 co so2 o3 o3_8h
     * @param $Cp 污染物浓度
     * @param bool $is_hour 是否小时类型
     * @return int
     */
    public function getIAQIhi($name, $Cp, $is_hour = false)
    {
        switch ($name) {
            case 'pm10':
                switch ($Cp) {
                    case $Cp > 500:
                        return 500;
                        break;
                    case $Cp > 420:
                        return 400;
                        break;
                    case $Cp > 350:
                        return 300;
                        break;
                    case $Cp > 250:
                        return 200;
                        break;
                    case $Cp > 150:
                        return 150;
                        break;
                    case $Cp > 50:
                        return 100;
                        break;
                    case $Cp > 0:
                        return 50;
                        break;
                }
                break;
            case 'pm25':
                switch ($Cp) {
                    case $Cp > 350:
                        return 500;
                        break;
                    case $Cp > 250:
                        return 400;
                        break;
                    case $Cp > 150:
                        return 300;
                        break;
                    case $Cp > 115:
                        return 200;
                        break;
                    case $Cp > 75:
                        return 150;
                        break;
                    case $Cp > 35:
                        return 100;
                        break;
                    case $Cp > 0:
                        return 50;
                        break;
                }
                break;
            case 'so2':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 650:
                            return 200;
                            break;
                        case $Cp > 500:
                            return 150;
                            break;
                        case $Cp > 150:
                            return 100;
                            break;
                        case $Cp > 0:
                            return 50;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 2100:
                            return 500;
                            break;
                        case $Cp > 1600:
                            return 400;
                            break;
                        case $Cp > 800:
                            return 300;
                            break;
                        case $Cp > 475:
                            return 200;
                            break;
                        case $Cp > 150:
                            return 150;
                            break;
                        case $Cp > 50:
                            return 100;
                            break;
                        case $Cp > 0:
                            return 50;
                            break;
                    }
                }
                break;
            case 'no2':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 3090:
                            return 500;
                            break;
                        case $Cp > 2340:
                            return 400;
                            break;
                        case $Cp > 1200:
                            return 300;
                            break;
                        case $Cp > 700:
                            return 200;
                            break;
                        case $Cp > 200:
                            return 150;
                            break;
                        case $Cp > 100:
                            return 100;
                            break;
                        case $Cp > 0:
                            return 50;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 750:
                            return 500;
                            break;
                        case $Cp > 565:
                            return 400;
                            break;
                        case $Cp > 280:
                            return 300;
                            break;
                        case $Cp > 180:
                            return 200;
                            break;
                        case $Cp > 80:
                            return 150;
                            break;
                        case $Cp > 40:
                            return 100;
                            break;
                        case $Cp > 0:
                            return 50;
                            break;
                    }
                }
                break;
            case 'co':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 120:
                            return 500;
                            break;
                        case $Cp > 90:
                            return 400;
                            break;
                        case $Cp > 60:
                            return 300;
                            break;
                        case $Cp > 35:
                            return 200;
                            break;
                        case $Cp > 10:
                            return 150;
                            break;
                        case $Cp > 5:
                            return 100;
                            break;
                        case $Cp > 0:
                            return 50;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 48:
                            return 500;
                            break;
                        case $Cp > 36:
                            return 400;
                            break;
                        case $Cp > 24:
                            return 300;
                            break;
                        case $Cp > 14:
                            return 200;
                            break;
                        case $Cp > 4:
                            return 150;
                            break;
                        case $Cp > 2:
                            return 100;
                            break;
                        case $Cp > 0:
                            return 50;
                            break;
                    }
                }
                break;
            case 'o3':
                switch ($Cp) {
                    case $Cp > 1000:
                        return 500;
                        break;
                    case $Cp > 800:
                        return 400;
                        break;
                    case $Cp > 400:
                        return 300;
                        break;
                    case $Cp > 300:
                        return 200;
                        break;
                    case $Cp > 200:
                        return 150;
                        break;
                    case $Cp > 160:
                        return 100;
                        break;
                    case $Cp > 0:
                        return 50;
                        break;
                }
                break;
            case 'o3_8h':
                switch ($Cp) {
                    case $Cp > 265:
                        return 300;
                        break;
                    case $Cp > 215:
                        return 200;
                        break;
                    case $Cp > 160:
                        return 150;
                        break;
                    case $Cp > 100:
                        return 100;
                        break;
                    case $Cp > 0:
                        return 50;
                        break;
                }
                break;
        }
    }

    /**
     * 表中与BPlo对应的空气质量分指数
     * @author yy by 2019/3/28
     * @param $name 污染物名称 pm10 pm25 no2 co so2 o3 o3_8h
     * @param $Cp 污染物浓度
     * @param bool $is_hour 是否小时类型
     * @return int
     */
    public function getIAQIlo($name, $Cp, $is_hour = false)
    {
        switch ($name) {
            case 'pm10':
                switch ($Cp) {
                    case $Cp > 500:
                        return 400;
                        break;
                    case $Cp > 420:
                        return 300;
                        break;
                    case $Cp > 350:
                        return 200;
                        break;
                    case $Cp > 250:
                        return 150;
                        break;
                    case $Cp > 150:
                        return 100;
                        break;
                    case $Cp > 50:
                        return 50;
                        break;
                    case $Cp > 0:
                        return 0;
                        break;
                }
                break;
            case 'pm25':
                switch ($Cp) {
                    case $Cp > 350:
                        return 400;
                        break;
                    case $Cp > 250:
                        return 300;
                        break;
                    case $Cp > 150:
                        return 200;
                        break;
                    case $Cp > 115:
                        return 150;
                        break;
                    case $Cp > 75:
                        return 100;
                        break;
                    case $Cp > 35:
                        return 50;
                        break;
                    case $Cp > 0:
                        return 0;
                        break;
                }
                break;
            case 'so2':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 650:
                            return 150;
                            break;
                        case $Cp > 500:
                            return 100;
                            break;
                        case $Cp > 150:
                            return 50;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 2100:
                            return 400;
                            break;
                        case $Cp > 1600:
                            return 300;
                            break;
                        case $Cp > 800:
                            return 200;
                            break;
                        case $Cp > 475:
                            return 150;
                            break;
                        case $Cp > 150:
                            return 100;
                            break;
                        case $Cp > 50:
                            return 50;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                }
                break;
            case 'no2':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 3090:
                            return 400;
                            break;
                        case $Cp > 2340:
                            return 300;
                            break;
                        case $Cp > 1200:
                            return 200;
                            break;
                        case $Cp > 700:
                            return 150;
                            break;
                        case $Cp > 200:
                            return 100;
                            break;
                        case $Cp > 100:
                            return 50;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 750:
                            return 400;
                            break;
                        case $Cp > 565:
                            return 300;
                            break;
                        case $Cp > 280:
                            return 200;
                            break;
                        case $Cp > 180:
                            return 150;
                            break;
                        case $Cp > 80:
                            return 100;
                            break;
                        case $Cp > 40:
                            return 50;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                }
                break;
            case 'co':
                if ($is_hour) {
                    switch ($Cp) {
                        case $Cp > 120:
                            return 400;
                            break;
                        case $Cp > 90:
                            return 300;
                            break;
                        case $Cp > 60:
                            return 200;
                            break;
                        case $Cp > 35:
                            return 150;
                            break;
                        case $Cp > 10:
                            return 100;
                            break;
                        case $Cp > 5:
                            return 50;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                } else {
                    switch ($Cp) {
                        case $Cp > 48:
                            return 400;
                            break;
                        case $Cp > 36:
                            return 300;
                            break;
                        case $Cp > 24:
                            return 200;
                            break;
                        case $Cp > 14:
                            return 150;
                            break;
                        case $Cp > 4:
                            return 100;
                            break;
                        case $Cp > 2:
                            return 50;
                            break;
                        case $Cp > 0:
                            return 0;
                            break;
                    }
                }
                break;
            case 'o3':
                switch ($Cp) {
                    case $Cp > 1000:
                        return 400;
                        break;
                    case $Cp > 800:
                        return 300;
                        break;
                    case $Cp > 400:
                        return 200;
                        break;
                    case $Cp > 300:
                        return 150;
                        break;
                    case $Cp > 200:
                        return 100;
                        break;
                    case $Cp > 160:
                        return 50;
                        break;
                    case $Cp > 0:
                        return 0;
                        break;
                }
                break;
            case 'o3_8h':
                switch ($Cp) {
                    case $Cp > 265:
                        return 200;
                        break;
                    case $Cp > 215:
                        return 150;
                        break;
                    case $Cp > 160:
                        return 100;
                        break;
                    case $Cp > 100:
                        return 50;
                        break;
                    case $Cp > 0:
                        return 0;
                        break;
                }
                break;
        }
    }
}